package com.ruoyi.jeeplus.modules.screen.frontdesk.screen_workstatus.service.impl;

import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_terminal_number.mapper.Frontdesk_screen_terminal_numberMapper;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_workstatus.mapper.Frontdesk_screen_workstatusMapper;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_workstatus.service.IFrontdesk_screen_workstatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Service
public class Frontdesk_screen_workstatusImpl implements IFrontdesk_screen_workstatusService {
    @Resource
    Frontdesk_screen_workstatusMapper frontdeskScreenWorkstatusMapper;
    @Autowired
    Frontdesk_screen_terminal_numberMapper frontdeskScreenTerminalNumberMapper;

    //4轮的方法
    public int four_using(int id,int date){
        String parentIdsById = frontdeskScreenTerminalNumberMapper.getParentIdsById(id);
        int i = calculateLevel(parentIdsById);
        String todayDate = getTodayDate(date);
        if (i == 4){

            //int two_number = a1(id, todayDate);
            int two_number = frontdeskScreenWorkstatusMapper.four_countRechargeRecords(id,todayDate);
            return two_number;
        }else {
            //转发到其他方法
            //随机子类
            String str = Integer.toString(id);
            int child = frontdeskScreenTerminalNumberMapper.selectRandomByParentId(str);
            String childString = Integer.toString(child);
            System.out.println(childString);
            //两轮的省市
            int i1 = frontdeskScreenWorkstatusMapper.countRecords_four(childString, todayDate);
            return i1;
        }

    }

    //两轮的方法
    public int using(int id,int date){
        String parentIdsById = frontdeskScreenTerminalNumberMapper.getParentIdsById(id);
        int i = calculateLevel(parentIdsById);
        String todayDate = getTodayDate(date);
        if (i == 4){

            //int two_number = a1(id, todayDate);
            int two_number = frontdeskScreenWorkstatusMapper.countRechargeRecords(id,todayDate);
            return two_number;
        }else {
            //转发到其他方法
            //随机子类
            String str = Integer.toString(id);
            int child = frontdeskScreenTerminalNumberMapper.selectRandomByParentId(str);
            String childString = Integer.toString(child);
            System.out.println(childString);
            //两轮的省市
            int i1 = frontdeskScreenWorkstatusMapper.countRecords_two(childString, todayDate);
            return i1;
        }

    }
    //计算层级
    public int calculateLevel(String parentIds) {
        // 按逗号切割字符串
        String[] ids = parentIds.split(",");
        // 返回数组的长度作为层级数量
        return ids.length;
    }
    //获取时间
    public String getTodayDate(int id) {
        LocalDate today = LocalDate.now();
        String result;
        switch (id) {
            case 0:
                //完成拼接数据查询
                result = "%";
                break;
            case 1:
                result = String.valueOf(today.getYear()); // 返回年
                result = result + "%";
                break;
            case 2:
                result = today.format(DateTimeFormatter.ofPattern("yyyy-MM")); // 返回年月
                result = result + "%";
                break;
            case 3:
                result = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 返回年月日
                result = result + "%";
                break;
            default:
                result = "无效的格式"; // 处理无效的参数
        }
        // System.out.println("result==" + result);
        return result;
    }
    public int a1(int id ,String date){

        int i = frontdeskScreenWorkstatusMapper.countRechargeRecords(id,date);
        return i;
    }


}
